﻿<!DOCTYPE>
<html ng-app="weChatMain">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, width=device-width, minimal-ui">
    <link href="/css/style.css" rel="stylesheet" type="text/css" />
	<link rel="stylesheet" type="text/css" href="/css/pensonel.css" />
    <script src="/framework/angular-wang-editor/dist/js/lib/jquery-1.10.2.min.js" type="text/javascript"></script>
    <script src="/framework/angular.min.js"></script>
    <title ng-bind="systemTitle"></title>
    <style type="text/css">
    .active{color: #39f; border:1px #39f solid !important;;}
    </style>
</head>

<body style=" background:#fff;"   ng-controller="weChatCtrl" ng-cloak>
<!--loading-->
        <div class="jisuloading" style="z-index:10001" ng-if="hasState"> 
         <div class="img"> 
             <center>
            <img src="/images/yemianloding.gif" width="90" height="90"/> 
            </center>
            {{loadmsg}}
         </div> 
     </div>
    <!-- 注册第一步-->
    <section class="content">

    <div class="signup ">
        <div class="formsignup">
        <div class="form-unit">
            <form class="login-was-successful ">
		        <div class="topsigunp"><img src="/images/logo.png" alt="" style="width: 70%;" ></div>
                <br/>
                <!------------填写绑定账号----------->
                  <!-----<div ng-if="bindBlock">------->
                <div  ng-if="bindBlock">
                    <div class="form-field">
                        <div class="form_title" style="font-size:15px;margin-bottom: 10px;">
                        您的微信还没有绑定学习平台，请先绑定！
                        </div>
                    </div>
                    <div class="form-field username">
                        <input type="text" name="user_name" placeholder="请输入绑定的账号" autocomplete="off" ng-model="params.account" class="form-control name" required ng-focus />
                      </div>
                      <div class="form-field">
                        <input type="password" placeholder="请输入密码" autocomplete="off"  name="user_pwd" ng-model="params.pwd" required class="form-control password" ng-minlength="6" ng-maxlength="16" ng-focus />
                      </div>
                      <div class="form-field" ng-if="binderror">
                        <div class="form_title" style=" color:Red; font-size:13px; margin-bottom:5px;">
                        {{binderrormsg}}
                        </div>
                    </div>
                      <button type="submit" class="btn form_btn" ng-click="bindWeChat()" style="background-color: #39f">立即绑定</button>

                      <div class="form-field">
                        <div class="form_title" style="text-align: left;font-size: 15px;margin-top: 20px;">
                        您还没有注册账号？
                        <br/>
                        <button type="button" class="btn form_btn wxdl" ng-click="wechatLogin()" style="border:1px solid #39f;color:#39f;background-color:#fff">{{wxzc}}</button>
                        </div>
                    </div>
                </div>
                <!------------提示错误信息----------->
                <!-----<div ng-if="!bindBlock">------->
                <div  ng-if="!bindBlock">
		        <div class="login-successful-img" style=" margin-top:50px;"><i class="iconfont"  style=" color:Red">&#xe6b9;</i></div>
                <div class="form-field">
                    <div class="form_title">{{unerr_msg}}
                    <br/><span class="form_title_yx"></span><br/> 
                    </div>
                </div>
                 <button type="submit" class="btn form_btn" onclick="window.location.href ='/login.html';">返回登录</button>
                 </div>
            </form>
        </div>
        </div>
        </div>
    </section>
    <script src="/framework/lodash.min.js" type="text/javascript"></script>
    <script src="/framework/restangular/restangular.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="/framework/angular-toastr/angular-toastr.min.js"></script>
    <script type="text/javascript" src="/framework/angular-toastr/angular-toastr.tpls.min.js"></script>
    <script src="/system/service/common.js" type="text/javascript"></script>
	<script type="text/javascript" src="/system/service/personnelpathservice.js"></script>
	<script src="https://cdn.bootcss.com/vConsole/3.3.0/vconsole.min.js"></script>
    <script type="text/javascript">
        //var vConsole = new VConsole();
        var mainApp = angular.module("weChatMain", ['Common', 'personnelmanagementS']).config(['$locationProvider', function ($locationProvider) {
            $locationProvider.html5Mode({
                enabled: true,
                requireBase: false//必须配置为false，否则<base href=''>这种格式带base连接的地址才能解析
            });
        } ]);

        mainApp.controller('weChatCtrl', ['$scope', 'RequestPopService', 'RequestUserService', 'sysUserInfo', 'Restangular', function ($scope, RPS, RM, sysUserInfo, Restangular) {
            Restangular.addFullRequestInterceptor(function(element, operation, route, url, headers, params, httpConfig) {     
                return {              
                    headers: {"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"}  
                };
            });
            $scope.loadmsg = "正在检查扫描结果。。。";
            //授权状态
            $scope.hasState = true;
            //展示的模块（默认是错误模块）
            $scope.bindBlock = false;
            //绑定模块错误显示
            $scope.binderror = false;
            //绑定模块错误提示
            $scope.binderrormsg = "";

            var value = sessionStorage.getItem("H5ID");
            if (value == undefined || value == null || value.length <= 0) {
                value = "";
            } else {
                value = JSON.parse(value);
            }
            $scope.params = {
                account: "",
                code: "",
                type: 1,
                pwd: "",
                oid: value
            };

            //错误信息
            $scope.unerr_msg = "";
            $scope.QueryString = function (fieldName) {
                /// <summary>
                ///   获得URL GET参数  
                /// </summary>
                /// <param name="fieldName" type="String">
                ///   参数名
                /// </param>
                /// <returns type="void" />如果不存在返回NULL
                var reg = new RegExp("(^|&)" + fieldName + "=([^&]*)(&|$)", "i");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]); return null;
            }
            var code = $scope.QueryString("code");
            var state = $scope.QueryString("state");

            //验证账号
            $scope.validateAccount = function () {
                RM.wechatlogin({ code: code, state: state, oid: value }).then(function (users) {
                    if (users.errorcode == 0) {
                        if (users.data == undefined || users.data == null) {
                            $scope.hasState = false;
                            $scope.unerr_msg = "账号或密码错误！";
                        } else {
                            sysUserInfo.setuserinfotoken(users.token);
                            //跳转app考勤页面

                            if ($scope.QueryString("url") != undefined) {

                                var url=$scope.QueryString("url");
                                if(url.indexOf('/sso')!=-1){
                                    window.location.href =url.split("/sso")[0]+"#/sso/" + users.data.user_ID+"/"+users.token;
                                }else{

                                    Restangular.addFullRequestInterceptor(function (element, operation, route, url, headers, params, httpConfig) {
                                        return {
                                            headers: { "X-Session-Token": users.token, "Content-Type": "application/x-www-form-urlencoded;charset=utf-8" }
                                        };
                                    });
                                    //获取用户信息
                                    RPS.personnelGetKey({ user_ID: users.data.user_ID }).then(function (retobj) {
                                        if (retobj.errorcode == 0) {
                                            retobj.data.userId = retobj.data.user_ID;
                                            retobj.data.username = retobj.data.user_Name;
                                            if (retobj.data.user_Img == "" || retobj.data.user_Img == null || retobj.data.user_Img == undefined) {
                                                retobj.data.user_Img = "/images/avatar.png";
                                            }
                                            if (retobj.data.userContact != undefined && retobj.data.userContact.unionid != undefined && retobj.data.userContact.unionid.length > 0) {
                                                retobj.data.openid = retobj.data.userContact.openId;
                                            } else {
                                                retobj.data.openid = "";
                                            }
                                            sysUserInfo.setuserinfo(retobj.data);
                                            window.location.href = $scope.QueryString('url').split("_").join("&");
                                        } else if (retobj.errorcode == "43") {
                                            $scope.binderror = true;
                                            $scope.binderrormsg = "企业套餐已过期，请续费";
                                        } else if (retobj.errorcode == "44") {
                                            $scope.binderror = true;
                                            $scope.binderrormsg = "此账号已离职/结业，无法登录！";
                                        }else{
                                            $scope.binderror = true;
                                            $scope.binderrormsg = retobj.errorcode+":"+retobj.errormsg;
                                        }
                                    });
                                }
                            } else {
                                window.location.href = "/test_login.html#" + users.data.user_ID+"/"+users.token;
                            }
                        }
                        //绑定人员信息
                    } else if (users.errorcode == 4) {

                        if (users.errormsg == undefined || users.errormsg == null || users.errormsg.length <= 0) {
                            $scope.hasState = false;
                            $scope.unerr_msg = "请先到微信开放平台绑定此公众号！";
                        } else {
                            $scope.hasState = false;
                            $scope.bindBlock = true;
                            $scope.params.code = users.errormsg;
                        }

                    } else if (users.errorcode == 25) {
                        $scope.hasState = false;
                        $scope.unerr_msg = "验证过期，请重新扫描登录";
                    } else if (users.errorcode == 26) {
                        $scope.hasState = false;
                        $scope.unerr_msg = "参数错误！";
                    } else if (users.errorcode == 27) {
                        $scope.hasState = false;
                        $scope.unerr_msg = users.errormsg;
                    } else if (users.errorcode == 44) {
                        $scope.hasState = false;
                        $scope.unerr_msg = users.errormsg;
                    } else {
                        $scope.hasState = false;
                        $scope.unerr_msg = "登录异常，请联系管理员！";
                    }
                }, function errorCallback() {
                    $scope.hasState = false;
                    $scope.unerr_msg = "连接服务器失败！";
                });
            }
            //未授权
            if (code == undefined || code == null || code.length <= 0) {
                $scope.hasState = false;
                $scope.unerr_msg = "微信授权失败";
                //已授权，请求后端验证账号
            } else {
                $scope.validateAccount();
            }

            //微信快速注册
            $scope.wxzc = "一键注册";
            $scope.wechatLogin = function () {
                $scope.wxzc = "注册中";
                $scope.hasState = true;
                $scope.loadmsg = "加载中";
                
                //alert(code);
                RM.wechatsBind({ code: $scope.params.code, orgid: value }).then(function (users) {
                    if (users.errorcode == 0) {
                        $scope.wxzc = "注册成功！";
                        users.data.userId = users.data.user_ID;
                        users.data.username = users.data.user_Name;

                        localStorage.setItem("userinfo", JSON.stringify(users.data));
                        var token = { outTime: new Date().getTime(), token: users.token }
                        localStorage.setItem("userinfo_token", JSON.stringify(token));

                        if ($scope.QueryString('url') == undefined || $scope.QueryString('url') == null) {
                            window.location.href = "/app/home.html?v=" + new Date().getTime();
                        } else {

                            var url=$scope.QueryString("url");
                            if(url.indexOf('/sso')!=-1){
                                window.location.href =url.split("/sso")[0]+"/#/sso/" + users.data.user_ID+"/"+users.token;
                            }else{
                                window.location.href = $scope.QueryString('url').split("_").join("&");
                            }
                        }
                    } else if (users.errorcode == 44) {
                        $scope.wxzc = users.errormsg;
                        $scope.hasState = false;
                    } else {
                        $scope.wxzc = "注册失败！";
                        //alert(users.errorcode+users.errormsg)
                        $scope.hasState = false;
                    }
                }, function () {
                    $scope.wxzc = "注册异常！";
                    $scope.hasState = false;
                });
            }

            //绑定用户信息
            $scope.bindWeChat = function () {
                if ($scope.params.account.length <= 0) {
                    $scope.binderror = true;
                    $scope.binderrormsg = "请输入账号！";
                    return;
                }
                if ($scope.params.pwd.length <= 0) {
                    $scope.binderror = true;
                    $scope.binderrormsg = "请输入密码！";
                    return;
                }
                RM.wechatBindings($scope.params).then(function (users) {
                    if (users.errorcode == 0) {
                        if (users.data != undefined && users.data != null) {
                            $scope.binderror = false;
                            $scope.binderrormsg = "";
                            sysUserInfo.setuserinfotoken(users.token);
                            //跳转app考勤页面
                            if ($scope.QueryString("url") != undefined) {
                                var url=$scope.QueryString("url");
                                if(url.indexOf('/sso')!=-1){
                                    
                                    window.location.href =url.split("/sso")[0]+"/#/sso/" + users.data.user_ID+"/"+users.token;
                                }else{
                                    Restangular.addFullRequestInterceptor(function (element, operation, route, url, headers, params, httpConfig) {
                                        return {
                                            headers: { "X-Session-Token": users.token, "Content-Type": "application/x-www-form-urlencoded;charset=utf-8" }
                                        };
                                    });
                                    //获取用户信息
                                    RPS.personnelGetKey({ user_ID: users.data.user_ID }).then(function (retobj) {
                                        if (retobj.errorcode == 0) {
                                            retobj.data.userId = retobj.data.user_ID;
                                            retobj.data.username = retobj.data.user_Name;
                                            if (retobj.data.user_Img == "" || retobj.data.user_Img == null || retobj.data.user_Img == undefined) {
                                                retobj.data.user_Img = "/images/avatar.png";
                                            }
                                            sysUserInfo.setuserinfo(retobj.data);
                                            if ($scope.QueryString('url') != undefined && $scope.QueryString('url') != null) {
                                                window.location.href = $scope.QueryString('url').split("_").join("&");
                                            } else {
                                                window.location.href = "/app/home.html?v=" + new Date().getTime();
                                            }
                                        } else if (retobj.errorcode == "43") {
                                            $scope.binderror = true;
                                            $scope.binderrormsg = "企业套餐已过期，请续费";
                                        } else if (retobj.errorcode == "44") {
                                            $scope.binderror = true;
                                            $scope.binderrormsg = "此账号已离职/结业，无法登录！";
                                        }else{
                                            $scope.binderror = true;
                                            $scope.binderrormsg = retobj.errorcode+":"+retobj.errormsg;
                                        }
                                    });
                                }
                            } else {
                                window.location.href = "/test_login.html#" + users.data.user_ID+"/"+users.token;
                            }
                        } else {
                            $scope.binderror = true;
                            $scope.binderrormsg = "账号或密码错误！";
                        }

                    } else if (users.errorcode == 12) {
                        $scope.binderror = true;
                        $scope.binderrormsg = "账号或密码错误！";
                    } else if (users.errorcode == 29) {
                        $scope.binderror = true;
                        $scope.binderrormsg = "该账号已绑定其他微信号！";
                    } else if (users.errorcode == 44) {
                        $scope.binderror = true;
                        $scope.binderrormsg = users.errormsg;
                    } else {
                        $scope.binderror = true;
                        $scope.binderrormsg = "系统错误！";
                    }
                }, function errorCallback() {
                    $scope.binderror = true;
                    $scope.binderrormsg = "网络异常！";
                });
            }

        } ]);

    </script>
    <script src="/system/service/userpathservice.js" type="text/javascript"></script>
     <script src="/system/service/personnelpathservice.js" type="text/javascript"></script>
</body>
</html>
